<template>
  <div class="login_box">
    <div class="login_content_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../../assets/logo.png" alt="">
      </div>

      <!-- 表单区域 -->
      <el-form class="login_form_box" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item label="" class="formInput" prop="username">
          <el-input prefix-icon="icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="" class="formInput" prop="password">
          <el-input prefix-icon="icon-lock-closed" type="password" v-model="loginForm.password"></el-input> 
        </el-form-item>

        <el-form-item label="" class="btn_box">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import {getdata} from '../../network/getdata'
export default {
  name:'login',
  data() {
    return {
      loginForm:{
        username:'admin',
        password:'123456'
      },
      //表单校验规则
      loginFormRules:{
        username:[
           { required: true, message: '请输入用户名', trigger: 'blur' },
           { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password:[
           {required: true, message: '请输入登录密码', trigger: 'blur' },
           { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //表单提交
    login() {
      //表单提交前的预验证
      this.$refs.loginFormRef.validate(valid => {
        // console.log(valid)
        if(!valid) {
          //表单数据不符合要求
          return ;
        }
        //符合要求 --- 发起请求
        getdata({
          method:'post',
          url:'login',
          data:this.loginForm
        }).then(result => {
          const res = result.data
          if(res.meta.status !== 200) {
            //登录失败
            this.$message.error('登录失败');
            return ;
          }
          //登录成功
          this.$message.success('登录成功')
          /**
           * 将登录成功 返回的 token 信息 保存到 sessionstorage中 
           * 不使用 locationstorage 的原因是因为 token 在 关闭浏览器之后就要失效
           */
          window.sessionStorage.setItem('token',res.data.token)
          //跳转到主页
          this.$router.push('/home')
        })
      })
    },
    //重置表单的值,并移除表单校验结果
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    }
  },
}
</script>

<style lang="less" scoped>
  .login_box {
    height: 100%;
    background-color: #2b4b6b;
  }
  .login_content_box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
  }
  // 头像盒子
  .avatar_box {
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 130px;
    height: 130px;
    padding: 10px;
    border-radius: 65px;
    overflow: hidden;
    box-shadow: 0 0 10px #ccc;
    background-color: #eee;
  }
  .avatar_box img {
    width: 100%;
    height: 100%;
  }
  // 表单
  .login_form_box{
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    padding: 0 10px;
  }
  .btn_box {
    text-align: right;
  }
</style>